package org.mms.service.impl;

import org.mms.exception.BusinessException;
import org.mms.pojo.dto.AnnouncementDto;
import org.mms.pojo.entity.Announcement;
import org.mms.pojo.request.AnnouncementRequest;
import org.mms.repository.AnnouncementRepository;
import org.mms.service.AnnouncementService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnnouncementServiceImpl implements AnnouncementService {
    @Autowired
    private AnnouncementRepository announcementRepository;

    @Override
    public List<Announcement> list() {
        return announcementRepository.findAll();
    }

    @Override
    public Announcement getById(Long id) {
        return announcementRepository.findById(id)
                .orElseThrow(() -> new BusinessException("公告不存在"));
    }

    @Override
    public Announcement create(AnnouncementRequest request) {
        Announcement entity = new Announcement();
        BeanUtils.copyProperties(request, entity);
        return announcementRepository.save(entity);
    }

    @Override
    public Announcement update(AnnouncementRequest request) {
        Announcement entity = announcementRepository.findById(request.getId())
                .orElseThrow(() -> new BusinessException("公告不存在"));
        BeanUtils.copyProperties(request, entity);
        return announcementRepository.save(entity);
    }

    @Override
    public void delete(Long id) {
        announcementRepository.deleteById(id);
    }
}